package com.sts.source.model;

import com.sts.source.StsSource;
import com.sts.source.impl.CsvSourceImpl;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class CsvSourceConfig extends BaseSourceConfig {

    private static final int DEFAULT_MAX_BUFFER_SIZE = 16777216;

    private String filePath;
    private char fieldSeparator = ',';
    private char quoteCharacter = '"';
    private boolean skipEmptyLines = true;
    private boolean detectBomHeader = true;
    private int maxBufferSize = DEFAULT_MAX_BUFFER_SIZE;

    // 文件有头
    public CsvSourceConfig(String filePath) {
        this.filePath = filePath;
    }

    public CsvSourceConfig(String filePath, int maxBufferSize) {
        this.filePath = filePath;
        this.maxBufferSize = Math.min(maxBufferSize, DEFAULT_MAX_BUFFER_SIZE);
    }

    public void setMaxBufferSize(int maxBufferSize) {
        this.maxBufferSize = Math.min(maxBufferSize, DEFAULT_MAX_BUFFER_SIZE);
    }

    public CsvSourceConfig(String filePath, char fieldSeparator, char quoteCharacter, boolean skipEmptyLines, boolean detectBomHeader, int maxBufferSize) {
        this.filePath = filePath;
        this.fieldSeparator = fieldSeparator;
        this.quoteCharacter = quoteCharacter;
        this.skipEmptyLines = skipEmptyLines;
        this.detectBomHeader = detectBomHeader;
        this.maxBufferSize = Math.min(maxBufferSize, DEFAULT_MAX_BUFFER_SIZE);
    }

    // 文件没有头
    public CsvSourceConfig(boolean hasHeader, List<String> headerList, String filePath) {
        super(hasHeader, headerList);
        this.filePath = filePath;
    }

    public CsvSourceConfig(boolean hasHeader, List<String> headerList, String filePath, int maxBufferSize) {
        super(hasHeader, headerList);
        this.filePath = filePath;
        this.maxBufferSize = Math.min(maxBufferSize, DEFAULT_MAX_BUFFER_SIZE);
    }

    public CsvSourceConfig(boolean hasHeader, List<String> headerList, String filePath, char fieldSeparator, char quoteCharacter, boolean skipEmptyLines, boolean detectBomHeader, int maxBufferSize) {
        super(hasHeader, headerList);
        this.filePath = filePath;
        this.fieldSeparator = fieldSeparator;
        this.quoteCharacter = quoteCharacter;
        this.skipEmptyLines = skipEmptyLines;
        this.detectBomHeader = detectBomHeader;
        this.maxBufferSize = Math.min(maxBufferSize, DEFAULT_MAX_BUFFER_SIZE);
    }

    @Override
    public StsSource buildImpl() {
        return new CsvSourceImpl(this);
    }
}
